﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using XcoAppSpaces.Contracts.Logging;

namespace XcoAppSpaces.Logging.TraceSource
{
	/// <summary>
	/// Logger class used for logging in the XcoAppSpace. Uses the .Net TraceSource class to output 
	/// the log messages. Settings for this Logger can be easily done in the App.config.
	/// </summary>
	public class TraceSourceLogger : IXcoLogger
	{
		private readonly System.Diagnostics.TraceSource ts;
		//private SourceLevels logLevel;

		/// <summary>
		/// Creates a new Logger.
		/// </summary>
		public TraceSourceLogger(string sourceName, SourceLevels logLevel)
		{
			//this.logLevel = logLevel;
			ts = new System.Diagnostics.TraceSource(sourceName, logLevel);
		}

		/// <summary>
		/// Logs a debug message (using TraceEventType.Verbose).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		public void Debug(String msg)
		{
			ts.TraceEvent(TraceEventType.Verbose, 0, msg);
		}

		/// <summary>
		/// Logs an Info message (using TraceEventType.Information).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		public void Info(String msg)
		{
			ts.TraceEvent(TraceEventType.Information, 0, msg);
		}

		/// <summary>
		/// Logs a warning message (using TraceEventType.Warning).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		public void Warn(String msg)
		{
			ts.TraceEvent(TraceEventType.Warning, 0, msg);
		}

		/// <summary>
		/// Logs an error message (using TraceEventType.Error).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		public void Error(String msg)
		{
			ts.TraceEvent(TraceEventType.Error, 0, msg);
		}

		/// <summary>
		/// Logs an error message (using TraceEventType.Error).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		/// <param name="ex">The exception that was thrown.</param>
		public void Error(String msg, Exception ex)
		{
			ts.TraceEvent(TraceEventType.Error, 0, msg + "\n" + ex);
		}

		/// <summary>
		/// Logs a fatal error message (using TraceEventType.Critical).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		public void Fatal(String msg)
		{
			ts.TraceEvent(TraceEventType.Critical, 0, msg);
		}

		/// <summary>
		/// Logs a fatal error message (using TraceEventType.Critical).
		/// </summary>
		/// <param name="msg">The message to be logged.</param>
		/// <param name="ex">The exception that was thrown.</param>
		public void Fatal(String msg, Exception ex)
		{
			ts.TraceEvent(TraceEventType.Critical, 0, msg + "\n" + ex);
		}
	}
}
